package sdk.org.apache.shiro.G2_使用.C02_Quickstart_详细;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

import sdk.org.apache.shiro.utils.ShiroUtils;

public class Quickstart {
    private static void run(Timer timer) throws Exception {
        ShiroUtils.init("shiro.ini");

        test("root", "secret");
        test("guest", "guest");
        test("presidentskroob", "12345");
        test("lonestarr", "vespa");
    }


    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run(timer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print("执行总用时");
            System.exit(0);
        }
    }    
    
    
    private static void test(final String username, final String password) throws Exception {
        ConsoleUtils.printPart("验证用户： " + username);
        try {
            ShiroUtils.login(username, password, true);

            Subject currentUser = SecurityUtils.getSubject();
            ConsoleUtils.log("Determine whether the user ["+currentUser.getPrincipal()+"] has a role [admin]: " + currentUser.hasRole("admin"));
            ConsoleUtils.log("Determine whether the user ["+currentUser.getPrincipal()+"] has a role [guest]: " + currentUser.hasRole("guest"));
            ConsoleUtils.log("Determine whether the user ["+currentUser.getPrincipal()+"] has a role [president]: " + currentUser.hasRole("president"));
            ConsoleUtils.log("Determine whether the user ["+currentUser.getPrincipal()+"] has a role [goodguy]: " + currentUser.hasRole("goodguy"));
            ConsoleUtils.log("Determine whether the user ["+currentUser.getPrincipal()+"] has a role [schwartz]: " + currentUser.hasRole("schwartz"));
            ConsoleUtils.log("Determine whether the user ["+currentUser.getPrincipal()+"] has a permission [lightsaber:weild]: " + currentUser.isPermitted("lightsaber:weild"));
            ConsoleUtils.log("Determine whether the user ["+currentUser.getPrincipal()+"] has a permission [winnebago:drive:eagle5]: " + currentUser.isPermitted("winnebago:drive:eagle5"));
        } finally {
            ShiroUtils.logout();
        }
    }
}
